package com.example.eventdemo.slice;

import com.example.eventdemo.CommonSubscribe;
import com.example.eventdemo.ResourceTable;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.aafwk.content.Operation;
import ohos.agp.components.Button;
import ohos.agp.components.Component;
import ohos.app.dispatcher.TaskDispatcher;
import ohos.app.dispatcher.task.TaskPriority;
import ohos.event.commonevent.*;
import ohos.hiviewdfx.HiLog;
import ohos.hiviewdfx.HiLogLabel;
import ohos.miscservices.pasteboard.IPasteDataChangedListener;
import ohos.miscservices.pasteboard.SystemPasteboard;
import ohos.rpc.RemoteException;

public class MainAbilitySlice extends AbilitySlice {
    static final HiLogLabel LABEL = new HiLogLabel(HiLog.LOG_APP, 0x0024, "MY_TAG");
    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_main);
        addEventListener();

        Button button = (Button) findComponentById(ResourceTable.Id_button);
        button.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                try {
                    Intent intent = new Intent();
                    Operation operation = new Intent.OperationBuilder().withAction("custom.event").build();
                    intent.setOperation(operation);
                    CommonEventData eventData = new CommonEventData(intent);
                    CommonEventManager.publishCommonEvent(eventData);
                } catch (RemoteException e) {}
            }
        });

        runGlobalTaskASync();

    }

    private void runGlobalTaskSync() {
        TaskDispatcher globalTaskDispatcher = getGlobalTaskDispatcher(TaskPriority.DEFAULT);
        globalTaskDispatcher.syncDispatch(new Runnable() {
            @Override
            public void run() {
                HiLog.info(LABEL, "后台同步任务1执行");
            }
        });
        globalTaskDispatcher.syncDispatch(new Runnable() {
            @Override
            public void run() {
                HiLog.info(LABEL, "后台同步任务2执行");
            }
        });
        globalTaskDispatcher.syncDispatch(new Runnable() {
            @Override
            public void run() {
                HiLog.info(LABEL, "后台同步任务3执行");
            }
        });
        HiLog.info(LABEL, "主线程任务执行");
    }

    private void addPasteListener() {
        SystemPasteboard pasteboard = SystemPasteboard.getSystemPasteboard(this.getContext());
        pasteboard.addPasteDataChangedListener(new IPasteDataChangedListener() {
            @Override
            public void onChanged() {
                // 剪贴板数据变化的回调
            }
        });
    }

    private void runGlobalTaskASync() {
        TaskDispatcher globalTaskDispatcher = getGlobalTaskDispatcher(TaskPriority.DEFAULT);
        globalTaskDispatcher.asyncDispatch(new Runnable() {
            @Override
            public void run() {
                HiLog.info(LABEL, "后台异步任务执行");
            }
        });
        HiLog.info(LABEL, "主线程任务执行");
    }

    private void addEventListener() {
        MatchingSkills matchingSkills = new MatchingSkills();
        matchingSkills.addEvent("custom.event");
        matchingSkills.addEvent(CommonEventSupport.COMMON_EVENT_SCREEN_OFF);
        CommonEventSubscribeInfo info = new CommonEventSubscribeInfo(matchingSkills);
        CommonSubscribe mySubscribe = new CommonSubscribe(info);
        try {
            CommonEventManager.subscribeCommonEvent(mySubscribe);
        } catch(RemoteException e) {

        }
    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }
}
